package com.buss.customer.controller;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.buss.core.controller.BaseController;
import com.buss.core.exception.WebException;
import com.buss.core.persistence.ResultList;
import com.buss.core.sysparam.SysStaticParam;
import com.buss.customer.model.CustomerContacts;
import com.buss.customer.model.CustomerContacts.Salutation;
import com.buss.customer.service.CustomerContactsService;

@Controller
@RequestMapping("/admin/customercontacts/")
public class CustomerContactsController extends BaseController {

	@Autowired
	private CustomerContactsService contactsService;
	
	@Override
	public ModelAndView listPage(String menuid) {
//		ModelAndView view = new ModelAndView("customercontacts/listpage");
//		view.addObject(SysStaticParam._MENUID_NAME, menuid);
		return null;
	}

	@RequestMapping("{type}")
	public ModelAndView editPage(@PathVariable String type, Long id){
		ModelAndView view = new ModelAndView("customercontacts/edit");
		CustomerContacts contacts = new CustomerContacts();
		
		if(EDIT_PAGE.equals(type)){
			contacts = contactsService.loadContacts(id);
		}else{
			contacts.setCustomerId(id);
			contacts.setSalutation(CustomerContacts.Salutation.man);
		}
		view.addObject("contacts", contacts);
		return view;
	}
	
	@RequestMapping("add")
	@ResponseBody
	public Map<String, Object> add(CustomerContacts contacts){
		contactsService.saveContacts(contacts);
		return SysStaticParam.resultMessage("新增单位联系人成功!");
	}
	
	@RequestMapping("edit")
	@ResponseBody
	public Map<String,Object> edit(CustomerContacts contacts){
		contactsService.editContacts(contacts);
		return SysStaticParam.resultMessage("修改单位联系人成功!");
	}
	
	@Override
	@RequestMapping("delete")
	@ResponseBody
	public Map<String, Object> delete(Long[] ids) throws WebException {
		contactsService.removeContacts(ids);
		return SysStaticParam.resultMessage("删除单位联系人成功!");
	}
	
	@RequestMapping("list")
	@ResponseBody
	public Map<String, Object> list(Long customerId,String name){
		ResultList<CustomerContacts> rs = contactsService.queryContacts(customerId, name);
		Map<String, Object> map = SysStaticParam.resultMessage("获取单位联系人信息成功!");
		map.put(SysStaticParam._RESULT, rs);
		return map;
	}

}
